package com.djtu.service.web.controller;

import com.djtu.exception.ServiceException;
import com.djtu.response.Result;
import com.djtu.service.pojo.ServiceOrder;
import com.djtu.service.pojo.vo.RepairServiceVo;
import com.djtu.service.service.ServiceService;
import com.djtu.settings.pojo.User;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class ServiceController {

    @Autowired
    private ServiceService serviceService;

    /**
     * 学生申请报修功能
     * @param vo 报修内容
     * @return 成功提示
     */
    @RequiresRoles("学生")
    @PostMapping("/repair.do")
    @ResponseBody
    public Result<Object> repairService(@RequestBody RepairServiceVo vo) throws ServiceException {
        serviceService.repairService(vo);
        return new Result<>().setCode(200).setMessage("报修成功");
    }

    /**
     * 根据用户 ID 及分页信息查询维修历史及数据总量
     *
     * @param user 用户 ID 及分页信息
     * @return 维修历史及数据总量
     */
    @RequiresRoles("学生")
    @RequestMapping("/getServiceHistory.do")
    @ResponseBody
    public Result<Object> getServiceHistoryList(@RequestBody User user) throws ServiceException {
        List<ServiceOrder> list = serviceService.getServiceHistoryList(user);
        Integer total = serviceService.getServiceHistoryListTotal(user.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        return new Result<>().setCode(200).setMessage("加载成功").setData(map);
    }

    /**
     * 获取全部维修历史
     *
     * @return 全部维修历史
     */
    @RequiresRoles("后勤管理员")
    @RequestMapping("/getServiceOrderList.do")
    @ResponseBody
    public Result<Object> getServiceHistoryList() throws ServiceException {
        List<ServiceOrder> list = serviceService.getAllServiceList();
        return new Result<>().setCode(200).setMessage("获取成功").setData(list);
    }

    /**
     * 确认完成维修
     *
     * @return 成功提示
     */
    @RequiresRoles("后勤管理员")
    @RequestMapping("/repairService.do")
    @ResponseBody
    public Result<Object> repairService(@RequestBody Map<String,String> idMap) throws ServiceException {
        serviceService.repairServiceOk(idMap.get("id"));
        return new Result<>().setCode(200).setMessage("确认成功");
    }

}
